package com.example.riseofthedefender;

import javax.microedition.khronos.opengles.GL10;

import android.graphics.PointF;
import android.util.FloatMath;

public class GunThing {

	public static float range = 100;
	public static float arch = 70;
	public static PointF pos = new PointF(0, 0);
	public static int duration = 0;

	
	public static void shoot(PointF startPoint)
	{
		duration = 35;
		pos.set(startPoint.x+Player.Player_wHlv, startPoint.y+Player.Player_hHlv);
	}
	
	public static void update(PointF startPoint) {
		if (duration <= 0)
			return;
		
		pos.set(startPoint.x + Player.Player_wHlv, startPoint.y + Player.Player_hHlv);
		
		float startX = pos.x;
		float startY = pos.y;
		
		startX +=  -GameVar.touchVector.y * (Math.random() * range * 2 - range);
		startY +=  -GameVar.touchVector.x * (Math.random() * range * 2 - range);
		
		Bullet.arrayList.get(Bullet.getNext())
		.shoot(startX,startY, GameVar.touchAngleR);
	}

	public static boolean checkCollisionArcToCircle(PointF testPos, float testRadius) {
		testPos = new PointF(testPos.x + testRadius, testPos.y + testRadius);
		float distance = MyMath.distance(pos, testPos);
		if (distance < testRadius) {
			return true;
		} else {
			if (distance < range + testRadius) {
				float toBallAngle = MyMath.angleR(pos, testPos);
				PointF toBallVector = new PointF(FloatMath.cos(toBallAngle), FloatMath.sin(toBallAngle));
				float dot = MyMath.dot(GameVar.touchVector, toBallVector);
				float addAngle = FloatMath.sin(testRadius / distance) * MyMath.R2D;
				if (Math.acos(dot) * MyMath.R2D < arch * 0.5f + addAngle) {
					return true;
				}
			}
		}
		return false;
	}

	public static void draw(GL10 gl) {
		if (duration <= 0)
			return;
		float newX = pos.x + GameVar.touchVector.x * range * 0.5f;
		float newY = pos.y + GameVar.touchVector.y * range * 0.5f;

		gl.glLoadIdentity();

		gl.glTranslatef(-Camera.pos.x,-Camera.pos.y,0.0f);
		gl.glTranslatef(newX+GameVar.touchVector.x * 20, newY+GameVar.touchVector.y * 20, 0.0f);
		gl.glRotatef(GameVar.touchAngleD, 0, 0, 1);
		gl.glScalef(range * 0.5f, range, 1.0f);
		gl.glRotatef(duration*5, 0, 0, 1);
		gl.glBindTexture(GL10.GL_TEXTURE_2D,TextureManager.CIRCLETRANS.textures[0]);
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, Square.vertices.length / 3);

		--duration;
	}
}
